<?php
class fieldModel extends baseModel{
    protected $table ='cms_field';  //字段表
    //增加模型字段->编辑数据库表字段
    public function addField($data = array()){
        //获取当前模型信息
        $info = model('module')->find('id = '.$data['mid'],'tables');
        if($data['tbdata']==1){
            $info_data['tables'] = $info['tables'] .'_data';
        }else{
            $info_data['tables'] = $info['tables'];
        }
        //查询是否有重复字段
        $condition['mid'] = $data['mid'];
        $condition['fields'] = $data['fields'];
        $fieldinfo = $this->find($condition);
        if($fieldinfo){
            return FALSE;
        }
        //字段类型
        $type = intval($data['type']);
        $leng = intval($data['flong']);// 计算表单长度
        switch ($type){
            case 2: //多行文本
                $type='text CHARACTER SET utf8 NOT NULL';
                break;
            case 3: //编辑器
                $type='mediumtext CHARACTER SET utf8 NOT NULL';
                break;
            case 7: //整形数字
                if($leng>10 || $leng<1 ){
                    $leng=10;
                }
                $type='int('.$leng.')';
                break;
            case 8: //货币保留2位
                $leng='9,2';
                $type='decimal('.$leng.')';
                break;
            default://1单行文本,2多行文本(<=250),4下拉列表,5单选框,6复选框
                if( $leng>255 || $leng<1 ){
                    $leng=255;
                } 
                $type= 'varchar('.$leng.') CHARACTER SET utf8 NOT NULL'; //普通
       }
       $sql="ALTER TABLE {$this->model->pre}{$info_data['tables']} ADD `{$data['fields']}` {$type}";
       $result = $this->query($sql);
       if($result){
           $this->insert($data); 
           return TRUE;
       }else{
           return FALSE;
       }
    }
    //编辑模型字段->数据库表字段
    public function fieldedit($id,$data = array()){
        $condition = array();
        $condition['id'] = $id;
        $data = postinput($data,array('fields'));
        $result = $this->update($condition,$data);
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //删除字段->数据库字段
    public function delField($fid){
        $info = $this->find('id='.$fid,'mid,tbdata,fields');  //模型信息
        $rel = model('module')->find('id='.$info['mid'],'tables');
        if($info['tbdata']==1){
            $info_data['tables'] = $rel['tables'] .'_data';
        }else{
            $info_data['tables'] = $rel['tables'];
        }
        $sql="alter table ".config('DB_PREFIX')."{$info_data['tables']} DROP COLUMN {$info['fields']}"; //执行删除操作
        $result = $this->query($sql);
        if($result){//删除模型信息
            $this->delete('id='.$fid);
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //扩展模型表单的生成
    public function fromInput($mid,$update = FALSE,$input_value = array()) {
        $mid  = intval($mid);
        $info = $this->select('mid='.$mid,'name,fields,type,inputext,inputhold,default_val,inputcss,inputmsg,validform,errormsg,ignores','neworder asc,id asc');
        $input= array();
        if($info){
            foreach ($info as $key=>$rs) {
                if($rs['inputcss']){//表单样式
                    $inputcss[$key] = ' '.$rs['inputcss'];
                }
                if($rs['validform']){//表单验证
                    if($rs['ignores'] == 1){//非必须输入只验证格式
                        $ignore[$key] = 'ignore="ignore" ';
                    }
                    $validform[$key] = $ignore[$key].'datatype="'.$rs['validform'].'" errormsg="'.$rs['errormsg'].'" ';
                }
                if($rs['inputext']){//表单扩展
                    $inputext[$key] = htmlspecialchars_decode($rs['inputext']).' ';
                }
                if($rs['inputhold']){
                    $js[$key] = htmlspecialchars_decode($rs['inputhold']);
                }
                if($update){//表单的值
                    $value[$key] = $input_value[$rs['fields']];
                }else{
                    $value[$key] = $rs['default_val'];
                }
                $inputname[$key] = 'id="'.$rs['fields'].'" name="'.$rs['fields'].'" ';
                $inputplus[$key] = $inputname[$key].$validform[$key].$inputext[$key];
                switch ($rs['type']) {
                    case 2: //多行
                        $inputstr[$key] = '<textarea class="textarea'.$inputcss[$key].'" '.$inputplus[$key].'type="text">'.$value[$key].'</textarea>';
                    break;
                    case 3: //多行不限制
                        $inputstr[$key] = '<textarea class="textarea'.$inputcss[$key].'" '.$inputplus[$key].'type="text">'.htmlspecialchars($value[$key]).'</textarea>';
                    break;
                    case 4: //下拉
                        $option ='<option value="">请选择选项</option>'; 
                        if($rs['default_val']){
                            $option_array = explode('#', $rs['default_val']);
                            foreach ($option_array as $value) {
                                $strarray = explode('|', $value);
                                $option .='<option value="'.$strarray[0].'"';
                                if($input_value[$rs['fields']] == $strarray[0]){
                                    $option .='selected="selected"';
                                }
                                $option .='>'.$strarray[1].'</option>'; 
                            } 
                        }
                        $inputstr[$key] = '<select class="select'.$inputcss[$key].'" '.$inputplus[$key].'>'.$option.'</select>'; 
                    break; 
                    case 5: //单选
                        if($rs['default_val']){
                            $option_array = explode('#', $rs['default_val']);
                            foreach ($option_array as $radiokey=>$value) {
                                $strarray = explode('|', $value);
                                if($input_value[$rs['fields']] == $strarray[0]){
                                    $checked[$radiokey] ='checked="checked" ';
                                }
                                $radio[$key][] ='<input class="radio'.$inputcss[$key].'" '.$checked[$radiokey].' value="'.$strarray[0].'" type="radio" name="'.$rs['fields'].'">'.$strarray[1]; 
                            }
                            $inputstr[$key] = implode('', $radio[$key]);  
                        }
                    break; 
                    case 6: //复选
                        if($rs['default_val']){
                            $option_array = explode('#', $rs['default_val']);
                            foreach ($option_array as $radiokey=>$value) {
                                $strarray = explode('|', $value);
                                if($input_value[$rs['fields']] == $strarray[0]){
                                    $checked[$radiokey] ='checked="checked" ';
                                }
                                $checkbox[$key][] ='<input class="checkbox'.$inputcss[$key].'" '.$checked[$radiokey].' value="'.$strarray[0].'" type="checkbox" name="'.$rs['fields'].'[]">'.$strarray[1]; 
                            }
                            $inputstr[$key] = implode('', $checkbox[$key]);  
                        }
                    break; 
                    default: //1单行,7整形数字,8货币
                        $inputstr[$key] = '<input class="input'.$inputcss[$key].'" '.$inputplus[$key].'type="text" value="'.$value[$key].'" />';
                    break;
                }
                $input[$key]['title'] = $rs['name'];
                $input[$key]['msg']   = $rs['inputmsg'];
                $input[$key]['input'] = $inputstr[$key].$js[$key];
            }
        }
        return $input; 
    }
}